########################################################################
# gui.py - Top-level GUI code for wickddup.py.  This is the interface
#	between core logic code and the rest of the GUI code.  It also
#	contains some utility code that doesn't fit well anywhere else.
#
# Created 2006.04.29 by Warren Young
#
# Copyright (c) 2006 by Pixel Corps.  This program may be used under
# the terms of the X11 license, a copy of which should have accompanied
# this program, in the LICENSE file.
########################################################################

import filepicker, upstatus

import wx

import thread


#### GetMainFrame ######################################################

def GetMainFrame():
	return wx.GetApp().GetTopWindow()


#### ShowFilePickerWindow ##############################################

def ShowFilePickerWindow(uploader):
	print "Creating file picker GUI..."
	frame = filepicker.FilePickerWindow(uploader)
	frame.Show()
	wx.GetApp().SetTopWindow(frame)


#### ShowUploadStatusWindow ############################################

def ShowUploadStatusWindow(uploader):
	oldFrame = GetMainFrame()
	
	print "Creating upload status window..."
	frame = upstatus.UploadStatusWindow(uploader)
	frame.Show()
	wx.GetApp().SetTopWindow(frame)

	if oldFrame:
		oldFrame.Close()


#### Shutdown ##########################################################
# Closes the top level window, which shuts the GUI down.

def Shutdown():
	wx.CallAfter(GetMainFrame().Shutdown)


#### UpdateProgress ####################################################
# Calls the function of the same name in the main frame window

def UpdateProgress(fileNum, percent):
	wx.CallAfter(GetMainFrame().UpdateProgress, fileNum, percent)


#### StartApp ##########################################################
# Create GUI and enter the main event loop.  Does not return until the
# user kills the GUI.

def StartApp(uploader):
	app = wx.PySimpleApp()

	if uploader.filePickerGUI:
		ShowFilePickerWindow(uploader)
	else:
		ShowUploadStatusWindow(uploader)

	try:
		print "MAIN LOOP START"
		app.MainLoop()
		print "MAIN LOOP EXIT"
	except Exception, err:
		print "Unhandled exception caught in main loop: ", err

